package com.kermitye.studentsys;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

    private Context context;
    private List<Student> datas;
    private OnItemClickListener onItemClickListener;
    private OnItemLongListener onItemLongListener;


    public StudentAdapter(Context context, List<Student> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnItemLongListener(OnItemLongListener listener) {
        this.onItemLongListener = listener;
    }


    @NonNull
    @Override
    public StudentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_student, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int position = holder.getAdapterPosition();
                    Student item = datas.get(position);
                    onItemClickListener.onItemClick(position, item);
                }
            }
        });

        holder.rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongListener != null) {
                    int position = holder.getAdapterPosition();
                    Student item = datas.get(position);
                    onItemLongListener.onItemLongClick(position, item);
                }
                return true;
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = datas.get(position);
        holder.tvNo.setText(student.id + "");
        holder.tvName.setText(student.name);
        holder.tvSpeciality.setText(student.speciality);
        holder.tvClazz.setText(student.clazz);
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView tvNo;
        TextView tvName;
        TextView tvSpeciality;
        TextView tvClazz;

        public ViewHolder(View view) {
            super(view);
            rootView = view;
            tvNo = view.findViewById(R.id.is_tv_no);
            tvName = view.findViewById(R.id.is_tv_name);
            tvSpeciality = view.findViewById(R.id.is_tv_speciality);
            tvClazz = view.findViewById(R.id.is_tv_clazz);
        }
    }


    static interface OnItemClickListener {
        void onItemClick(int position, Student item);
    }

    static interface OnItemLongListener {
        void onItemLongClick(int position, Student item);
    }

}

